<?php
/**
 * Created by PhpStorm.
 * User: zgh
 * Date: 2021-07-21
 * Time: 11:13
 */

namespace app\admin\controller;

use app\admin\service\RoleService;
use app\common\Annotation\ControllerAnnotation;
use app\common\Annotation\NodeAnotation;
use app\common\controller\BackendController;
use think\facade\Request;

/**
 * @ControllerAnnotation(title="角色权限管理",auth=2)
 * Class RoleController
 * @package app\admin\controller
 */
class RoleController extends BackendController
{
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if (Request::isAjax()) {
            $result = RoleService::getList(Request::param());
            return success_json_response("成功", $result['dataList'], ['count' => $result['total']]);
        } else {
            return $this->fetch();
        }
    }

    /**
     * @NodeAnotation(title="添加")
     * @return mixed|\think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function add()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $res = RoleService::save($params);
            if ($res) {
                return success_json_response("修改成功", ["id" => $res]);
            } else {
                return json_response("修改失败");
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="修改")
     * @return mixed|\think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function edit()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $res = RoleService::save($params);
            return $res ? success_json_response("修改成功", ["id" => $res]) : json_response("修改失败");
        }
        $model = RoleService::getByID($params['id']);
        $this->assign("model", $model);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     * @return \think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function del()
    {
        if (Request::isAjax()) {
            $res = RoleService::del(Request::param());
            return $res ? success_json_response("删除成功") : json_response("删除失败");
        }
    }

    /**
     * @NodeAnotation(title="授权")
     * @return mixed|\think\response\Json
     */
    public function authorize()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            if (isset($params['is_save'])) {
                $res = RoleService::authorizeSave($params);
                return $res ? success_json_response("授权成功") : json_response("授权失败");
            } else {
                $list = RoleService::getAuthorizeNodeListById($params['id']);
                return success_json_response("获取成功", $list);
            }
        }
        $model = RoleService::getByID($params['id']);
        $this->assign("model", $model);
        return $this->fetch();
    }


}